package com.fw.ui.bao;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.service.bean.EntryBean;
import com.fw.service.bean.SearchCriteriaBean;
import com.fw.service.bean.SearchResultBean;
import com.fw.service.serviceprofile.ServiceProfileService;
import com.fw.service.serviceprofile.bean.SearchServiceProfileBean;
import com.fw.service.serviceprofile.bean.ServiceProfileBean;
import com.fw.ui.service.ServiceExecutor;
import com.fw.ui.service.ServiceOrder;

@Component
@Scope(SCOPE_PROTOTYPE)
public class ServiceProfileBAO extends AbstractBAO implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    private ServiceProfileService serviceProfileService;

    @SuppressWarnings("unchecked")
    public Map<String, Serializable> createServiceProfile(ServiceProfileBean serviceProfileBean) throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.serviceProfileService);
	    serviceOrder.setMethodName("createServiceProfile");
	    serviceOrder.setParameterTypes(new Class[] { ServiceProfileBean.class });
	    serviceOrder.setParameterValues(new Object[] { serviceProfileBean });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		List<EntryBean<String, Serializable>> entryBeans = (List<EntryBean<String, Serializable>>) serviceOrder.getReturnValue();
		if (entryBeans != null) {
		    Map<String, Serializable> map = new HashMap<String, Serializable>();
		    for (EntryBean<String, Serializable> entryBean : entryBeans) {
			map.put(entryBean.getKey(), entryBean.getValue());
		    }
		    return map;
		}
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }

    public Boolean checkUniqueServiceId(String merchantId) throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.serviceProfileService);
	    serviceOrder.setMethodName("checkUniqueServiceId");
	    serviceOrder.setParameterTypes(new Class[] { String.class });
	    serviceOrder.setParameterValues(new Object[] { merchantId });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		Boolean result = (Boolean) serviceOrder.getReturnValue();
		return result;
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }

    @SuppressWarnings("unchecked")
    public SearchResultBean<ServiceProfileBean> selectServiceProfile(SearchCriteriaBean<SearchServiceProfileBean> searchCriteriaBean)
	    throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.serviceProfileService);
	    serviceOrder.setMethodName("selectServiceProfile");
	    serviceOrder.setParameterTypes(new Class[] { SearchCriteriaBean.class });
	    serviceOrder.setParameterValues(new Object[] { searchCriteriaBean });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		SearchResultBean<ServiceProfileBean> result = (SearchResultBean<ServiceProfileBean>) serviceOrder.getReturnValue();
		return result;
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }

    public byte[] exportServiceProfileAsCSV(SearchCriteriaBean<SearchServiceProfileBean> searchCriteriaBean) throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.serviceProfileService);
	    serviceOrder.setMethodName("exportServiceProfileAsCSV");
	    serviceOrder.setParameterTypes(new Class[] { SearchCriteriaBean.class });
	    serviceOrder.setParameterValues(new Object[] { searchCriteriaBean });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, exportTimeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		byte[] result = (byte[]) serviceOrder.getReturnValue();
		return result;
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }

    public ServiceProfileBean getServiceProfile(Long id) throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.serviceProfileService);
	    serviceOrder.setMethodName("getServiceProfile");
	    serviceOrder.setParameterTypes(new Class[] { Long.class });
	    serviceOrder.setParameterValues(new Object[] { id });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		ServiceProfileBean result = (ServiceProfileBean) serviceOrder.getReturnValue();
		return result;
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }

    @SuppressWarnings("unchecked")
    public Map<String, Serializable> updateServiceProfile(ServiceProfileBean serviceProfileBean) throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.serviceProfileService);
	    serviceOrder.setMethodName("updateServiceProfile");
	    serviceOrder.setParameterTypes(new Class[] { ServiceProfileBean.class });
	    serviceOrder.setParameterValues(new Object[] { serviceProfileBean });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		List<EntryBean<String, Serializable>> entryBeans = (List<EntryBean<String, Serializable>>) serviceOrder.getReturnValue();
		if (entryBeans != null) {
		    Map<String, Serializable> map = new HashMap<String, Serializable>();
		    for (EntryBean<String, Serializable> entryBean : entryBeans) {
			map.put(entryBean.getKey(), entryBean.getValue());
		    }
		    return map;
		}
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }

    @SuppressWarnings("unchecked")
    public Map<String, Serializable> deleteServiceProfile(ServiceProfileBean serviceProfileBean) throws Exception {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.serviceProfileService);
	    serviceOrder.setMethodName("deleteServiceProfile");
	    serviceOrder.setParameterTypes(new Class[] { ServiceProfileBean.class });
	    serviceOrder.setParameterValues(new Object[] { serviceProfileBean });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		List<EntryBean<String, Serializable>> entryBeans = (List<EntryBean<String, Serializable>>) serviceOrder.getReturnValue();
		if (entryBeans != null) {
		    Map<String, Serializable> map = new HashMap<String, Serializable>();
		    for (EntryBean<String, Serializable> entryBean : entryBeans) {
			map.put(entryBean.getKey(), entryBean.getValue());
		    }
		    return map;
		}
	    }
	} catch (Exception e) {
	    throw e;
	}
	return null;
    }

}
